// category.js
import {Category} from 'category-model.js'
var category = new Category

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentMenuIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._loadData();
  },

  _loadData: function() {
    category.getAllCategory((data)=>{
      
      this.setData({
        "categoryTypeArr":data
      });

      var firstCategory = data[0];
      category.getProductsByCateId(firstCategory.id, (data)=>{
        this.setData({
          "categoryProducts" : {
            "topImgUrl":firstCategory.img.url,
            "title":firstCategory.name,
            "procucts":data
          }
        });
      })
    })


  },

  changeCategory: function(event) {
    console.log(event);
    var idx = category.getDataset(event, 'index');
    var cateId = category.getDataset(event, 'id');
    var name = category.getDataset(event, 'name');
    var topImgUrl = category.getDataset(event, 'img');

    this.setData({
      "currentMenuIndex": idx
    });

    category.getProductsByCateId(cateId, (data) => {
      this.setData({
        "categoryProducts": {
          "topImgUrl": topImgUrl,
          "title": name,
          "procucts": data
        }
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
})